package com.alstudio.utils.distributor.versioncheck;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.alstudio.utils.android.AndroidUtils;
import com.alstudio.utils.android.net.download.ALHttpDownloadListener;
import com.alstudio.utils.android.net.download.ALHttpDownloadManager;
import com.alstudio.utils.android.net.download.ALHttpDownloadTask;
import com.alstudio.utils.android.net.download.ALHttpDownloadTask.ALHttpDownloadErrorCode;
import com.alstudio.utils.android.notification.ALNotification;
import com.alstudio.utils.android.notification.ALNotification.ALNotificationType;
import com.alstudio.utils.android.notification.ALNotificationManager;
import com.alstudio.utils.log.Mylog;

public class ALUpdateApkManager {

	private static Context mContext;

	public static void startDownloadApk(final Context context,
			final String url, final String path, final String name) {
		mContext = context;
		ALHttpDownloadManager.startDownload(context, url, path, name,
				mApkDownloadListener);
	}

	/**
	 * 用于接收下载APK更新的事件通知
	 */
	private static ALHttpDownloadListener mApkDownloadListener = new ALHttpDownloadListener() {

		private ALNotification n = null;
		private int progressed = 0;

		@Override
		public void onUpdateProcess(final ALHttpDownloadTask info) {
			// TODO Auto-generated method stub
			Mylog.e("下载进度: " + info.getDownloadPercent());
			new Handler().post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if (progressed != info.getDownloadPercent()) {
						progressed = info.getDownloadPercent();
						updateProgressNotification(mContext, n,
								info.getDownloadPercent(), info.getFilePath(),
								info.getFileName());
					}
				}
			});

		}

		@Override
		public void onDownloadFinish(ALHttpDownloadTask info) {
			// TODO Auto-generated method stub
			ALNotificationManager.cancelNotification(mContext, n);
		}

		@Override
		public void onPreDownload(final String name) {
			// TODO Auto-generated method stub
			Mylog.e("准备开始下载");
			n = showNotification(mContext, name);
		}

		@Override
		public void onDownloadFail(ALHttpDownloadErrorCode code, String name) {
			// TODO Auto-generated method stub
			Mylog.e("下载失败:" + code);
			showDownloadFailNotification(mContext, n);
			
		}
	};

	private static ALNotification showNotification(Context context,
			final String name) {
		{
			ALNotification builder = new ALNotification(context,
					ALNotificationType.AL_NOTIFICATION_TYPE_PROGRESS,
					"准备开始下载更新文件");
			if (!TextUtils.isEmpty(name)) {
				builder.setContentTitle("即将开始开始下载 " + name);
			} else {
				builder.setContentTitle("正在获取文件名");
			}
			builder.setContentText("0%");
			
			builder.setId(10086);

			builder.setContentIntent(ALNotification.backToApp(context));
			builder.setProgress(100, 0, false);

			ALNotificationManager.getAndroidNotificationManager(context)
					.notify(builder.getId(), builder.build());

			return builder;
		}
	}

	private static void updateProgressNotification(Context context,
			ALNotification notification, int progress, String path, String name) {

		notification.setProgress(100, progress, false);
		notification.setContentTitle("正在下载" + name);

		notification.setContentText(progress + "%");

		if (progress == 100) {
			notification.setContentTitle(name + "下载已完成");
			notification.setContentText("点击更新");

			AndroidUtils.installApk(context, path);
		}

		ALNotificationManager.getAndroidNotificationManager(context).notify(
				notification.getId(), notification.build());
	}

	private static void showDownloadFailNotification(Context context,
			ALNotification notification) {

		notification.setType(ALNotificationType.AL_NOTIFICATION_TYPE_NORMAL);
		notification.setContentTitle("下载失败");

		notification.setContentText("点击重试");

		ALNotificationManager.getAndroidNotificationManager(context).notify(
				notification.getId(), notification.build());
	}
}
